﻿using MyCommunity.BusinessLogic;
using System;
using MyCommunity.Models;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web;

public static class CacheController {

    private static Cache cache = HttpRuntime.Cache;

    private static string USERCACHE = "USERCACHE";

    private static Dictionary<string, string> dictionary = new Dictionary<string, string>();

    public static List<User> GetUser(IUserService service) {

        lock(typeof(CacheController)) {

            var user = cache[USERCACHE];

            if (user == null)
            {
                user = service.GetAllUsers();
                cache.Insert(USERCACHE, user);
            }

            return  (List<User>) user;
        }

    }

    public static void deleteUserCache()
    {

        lock (typeof(CacheController))
        {
            cache.Remove(USERCACHE);
        }

    }

    public static string getGUID(String mail)
    {
        lock (typeof(CacheController))
        {
            if (dictionary.ContainsKey(mail))
            {
                return dictionary[mail];
            }

            return null;
        }
    }

    public static void addGUID(User user, String guid) {
        lock (typeof(CacheController))
        {
            dictionary.Add(user.Mail, guid);
        }
    }

    public static void removeGUID(String mail)
    {
        lock (typeof(CacheController))
        {
            dictionary.Remove(mail);
        }
    }

}